import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/material.dart';
import 'package:pc/Extension/special_text/at_text.dart';
import 'package:pc/Extension/special_text/dollar_text.dart';
import 'package:pc/Extension/special_text/emoji_text.dart';
import 'package:pc/Extension/special_text/image_text.dart';

final mySpecialTextSpanBuilder = _MySpecialTextSpanBuilder();

class _MySpecialTextSpanBuilder extends SpecialTextSpanBuilder {
  /// whether show background for @somebody
  final bool showAtBackground;
  final bool showAt;
  final TextStyle? style; //关于字体的设置
  final bool defaultStyle; //是否使用自定义的字体设置
  final bool enableGif;

  _MySpecialTextSpanBuilder({
    this.showAtBackground = false,
    this.showAt = true,
    this.style,
    this.defaultStyle = true,
    this.enableGif = false,
  });

  @override
  TextSpan build(String data, {TextStyle? textStyle, onTap}) {
    TextSpan result = super.build(data, textStyle: defaultStyle ? textStyle : style, onTap: onTap);
    return result;
  }

  @override
  SpecialText? createSpecialText(String? flag, {TextStyle? textStyle, SpecialTextGestureTapCallback? onTap, required int index}) {
    if (flag == null || flag == "") return null;

    ///index is end index of start flag, so text start index should be index-(flag.length-1)
    if (showAt && (isStart(flag, AtText.flag) || isStart(flag, AtText.cnFlag))) {
      return AtText(textStyle!, onTap!, start: index - (AtText.flag.length - 1), showAtBackground: showAtBackground);
    } else if (isStart(flag, EmojiText.flag)) {
      return EmojiText(textStyle!, start: index - (EmojiText.flag.length - 1));
    } else if (isStart(flag, DollarText.flag)) {
      return DollarText(textStyle!, onTap!, start: index - (DollarText.flag.length - 1));
    } else if (isStart(flag, ImageText.flag)) {
      return ImageText(textStyle!, start: index - (ImageText.flag.length - 1), onTap: (parameter) => onTap?.call(parameter));
    }
    return null;
  }
}

class SpecialTextStyle {
  late TextRange textRange;
}
